package com.qf.xiaomi.dao.impl;

import com.qf.xiaomi.dao.UserDao;
import com.qf.xiaomi.pojo.TbAddress;
import com.qf.xiaomi.pojo.User;
import com.qf.xiaomi.utils.DruidDButils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

/**
 * @Created by 马英超 on 2022/8/15 19:45
 * @author： 马英超
 * @version：v1.0
 * @description:
 */
public class UserDaoImpl implements UserDao {


    /**
     * 用户名设置唯一，进行注册
     * @param user
     * @return
     */
    @Override
    public boolean registeredUser(User user) {
        String sql=" insert into tb_user(username,password ,email,gender,code,register_date)  VALUES(?,?,?,?,?,NOW())";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final int insetResult = queryRunner.update(DruidDButils.getDruidConn(), sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getGender(), user.getCode());
           if (insetResult>0)
               return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据用户名查询用户信息，返回一个user对象
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        String sql="select * from tb_user t where t.username=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final User user = queryRunner.query(DruidDButils.getDruidConn(), sql, new BeanHandler<>(User.class), username);
       if (user!=null)
           return user;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据邮箱、激活码查询是否存在用户
     * @param email
     * @param code
     * @return Boolean
     */

    @Override
    public boolean activeQuery(String email,String code ) {
         int result=0;
        String sql ="select count(0) from tb_user t where t.email=? and t.code=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
             Object query=queryRunner.query(DruidDButils.getDruidConn(), sql, new ScalarHandler(), email, code);
             final String s = String.valueOf(query);
              result= Integer.parseInt(s);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(result>0)
            return true;
        return false;
    }

    /**
     * 根据邮箱和激活码更新用户标识flag
     * @param email
     * @param code
     * @return Boolean
     */
    @Override
    public boolean updateFlag(String email, String code) {
         int result=0;
        String sql ="update tb_user t set flag=1 where t.email=? and t.code=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
           result = queryRunner.update(DruidDButils.getDruidConn(), sql, email, code);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (result>0)
            return true;
        return false;
    }

    /**
     * 根据用户id查询地址信息
     * @param uid
     * @return
     */
    @Override
    public List<TbAddress> queryAddressByUid(int uid) {
        String sql="select * from tb_address ta  where ta.uid=?";
        QueryRunner queryRunner=new QueryRunner();
        try {
            final List<TbAddress> addressList = queryRunner.query(DruidDButils.getDruidConn(), sql, new BeanListHandler<>(TbAddress.class), uid);
           if (addressList.size()>0)
               return addressList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
